import re

import allure

from basepage.BasePage import BasePage
from utils.log import LoggerUtils


class 衬衫大阔Page(BasePage):
    def __init__(self, page):
        super().__init__(page)

    def T恤_修身(self):
        # 点击快捷设计
        self.page.get_by_role("menuitem", name="快捷设计").click(force=True)
        # 点击创意组合
        self.click_locator('//*[@id="app"]/div/section/main/aside/div/div[2]/div/div/div/img', force=True)
        # 点击T恤
        self.click_get_by_role(role_name=('button','衬衫'))
        self.page.wait_for_timeout(250)
        # 选择全部分类
        self.hover_locator('//span[text()="全部 "]',num=2)
        # 点击修身分类
        self.click_get_by_role(role_name=("menuitem",'大阔'))
        self.page.wait_for_timeout(450)
        # 获取所有的分页数
        numb = int(self.locator('//li[@class="number"]',num=6).inner_text())
        LoggerUtils().logger().info(f'全部分页数：========== {numb}')
        # 循环所有的页数
        for j in range(100,numb-1):
            # 每页循环10款
            for i in range(0,10):
                # 点击款式
                self.click_locator(f'[id="\\3{i} "]')
                # 等待加载模型三秒
                self.page.wait_for_timeout(4500)
                try:
                    add_button = self.page.locator("//span[text()='您还没有添加装饰件']")
                    add_button.wait_for(timeout=500,state='visible')
                except Exception:
                # 如果 "添加" 按钮不可见或不存在，则点击 "查看更多"


                    Collar_number= self.locator('//span[@class="title-num text-xl pl-[0.35rem] pr-[0.35rem]"]').inner_text()
                    pattern = r'（(\d+)）'
                    match = re.search(pattern, Collar_number)
                    number = int(match.group(1))
                    LoggerUtils().logger().info(f'领子的数量：========== {number}')

                    # 点击领子更多
                    self.click_locator('//span[text()="查看更多"]', force=True)
                    # 循环所有的领子
                    for d in range(2, number+2):
                        # 获取所有的领子名称
                        text = self.locator('//span[@class="product-title text-base truncate ..."]',num=d).inner_text()
                        print(f'部件名称：========== {text}')
                        # 将取到的领子名字赋值到元素定位方式里面
                        self.click_get_by_title(f"{text}",exact=True,force=True)

                        # 更换完领子之后等待加载
                        self.page.wait_for_timeout(2000)
                        self.click_get_by_role(role_name=('button', '旋转180度'), force=True)
                        self.page.wait_for_timeout(500)

                    #     点击领子返回上一级
                    self.click_get_by_text(f"{Collar_number}", force=True)
                    try:
                        # 如果前后片存在
                        Collar_number2 = self.locator('//span[@class="title-num text-xl pl-[0.35rem] pr-[0.35rem]"]',num=2).inner_text()
                        pattern = r'（(\d+)）'
                        match = re.search(pattern, Collar_number2)
                        number2 = int(match.group(1))
                        LoggerUtils().logger().info(f'前后上片的数量：========== {number2}')
                        # 点击前后上片
                        self.click_locator('//span[text()="查看更多"]',num=2, force=True)
                        if number2 !=1:
                            # 循环点击前后上片
                            for d in range(2, number2+2):
                                # 获取所有的前后上片名称
                                text = self.locator('//span[@class="product-title text-base truncate ..."]',num=d).inner_text()
                                print(f'部件名称：========== {text}')
                                # 将取到的前后上片名字赋值到元素定位方式里面
                                self.click_get_by_title(f"{text}",exact=True,force=True)

                                # 更换完前后上片之后等待加载
                                self.page.wait_for_timeout(2000)
                                self.click_get_by_role(role_name=('button', '旋转180度'), force=True)
                                self.page.wait_for_timeout(500)
                            # 点击前后排片返回上一级
                            self.click_get_by_text(f"{Collar_number2}", force=True)
                            self.page.wait_for_timeout(300)

                        Collar_number3 = self.locator(
                            '//span[@class="title-num text-xl pl-[0.35rem] pr-[0.35rem]"]',
                            num=3).inner_text()
                        pattern = r'（(\d+)）'
                        match = re.search(pattern, Collar_number3)
                        number3 = int(match.group(1))
                        LoggerUtils().logger().info(f'袖子的数量：========== {number3}')
                        self.click_locator('//span[text()="查看更多"]', num=3, force=True)
                        #  循环点击袖子
                        for d in range(2, number3 + 2):
                            # 获取所有的袖子名称
                            text = self.locator('//span[@class="product-title text-base truncate ..."]',
                                                num=d).inner_text()
                            print(f'部件名称：========== {text}')
                            # 将取到的下摆名字赋值到元素定位方式里面
                            self.click_get_by_title(f"{text}", exact=True, force=True)

                            self.page.wait_for_timeout(2000)

                            self.click_get_by_role(role_name=('button', '旋转180度'), force=True)
                            self.page.wait_for_timeout(500)

                        # 点击袖子返回上一级
                        self.click_get_by_text(f"{Collar_number3}", force=True)
                        self.page.wait_for_timeout(600)

                        Collar_number4 = self.locator(
                            '//span[@class="title-num text-xl pl-[0.35rem] pr-[0.35rem]"]',
                            num=4).inner_text()
                        pattern = r'（(\d+)）'
                        match = re.search(pattern, Collar_number4)
                        number4 = int(match.group(1))
                        LoggerUtils().logger().info(f'下摆的数量：========== {number4}')
                        self.click_locator('//span[text()="查看更多"]', num=4, force=True)
                        #  循环点击下摆
                        for d in range(2, number4 + 2):
                            # 获取所有的下摆名称
                            text = self.locator('//span[@class="product-title text-base truncate ..."]',
                                                num=d).inner_text()
                            print(f'部件名称：========== {text}')
                            # 将取到的下摆名字赋值到元素定位方式里面
                            self.click_get_by_title(f"{text}", exact=True, force=True)

                            self.page.wait_for_timeout(2000)

                            self.click_get_by_role(role_name=('button', '旋转180度'))
                            self.page.wait_for_timeout(500)



                    except Exception:

                        # 点击袖子返回上一级
                        # self.click_get_by_text(f"{Collar_number2}")
                        Collar_number5 = self.locator('//span[@class="title-num text-xl pl-[0.35rem] pr-[0.35rem]"]',
                                                      num=2).inner_text()
                        pattern = r'（(\d+)）'
                        match = re.search(pattern, Collar_number5)
                        number5 = int(match.group(1))
                        LoggerUtils().logger().info(f'袖子的数量：========== {number5}')
                        self.click_locator('//span[text()="查看更多"]',num=2)
                        #  循环点击袖子
                        for d in range(2, number5+2):
                            # 获取所有的袖子名称
                            text = self.locator('//span[@class="product-title text-base truncate ..."]',num=d).inner_text()
                            print(f'部件名称：========== {text}')
                            # 将取到的下摆名字赋值到元素定位方式里面
                            self.click_get_by_title(f"{text}",exact=True,force=True)

                            self.page.wait_for_timeout(2000)
                            self.click_get_by_role(role_name=('button', '旋转180度'))
                            self.page.wait_for_timeout(500)

                        # 点击袖子返回上一级
                        self.click_get_by_text(f"{Collar_number5}")
                        self.page.wait_for_timeout(600)
                        Collar_number6 = self.locator('//span[@class="title-num text-xl pl-[0.35rem] pr-[0.35rem]"]',
                                                      num=3).inner_text()
                        pattern = r'（(\d+)）'
                        match = re.search(pattern, Collar_number6)
                        number6 = int(match.group(1))
                        LoggerUtils().logger().info(f'下摆的数量：========== {number6}')
                        self.click_locator('//span[text()="查看更多"]', num=3, force=True)
                        #  循环点击下摆
                        for d in range(2, number6 + 2):
                            # 获取所有的下摆名称
                            text = self.locator('//span[@class="product-title text-base truncate ..."]',
                                                num=d).inner_text()
                            print(f'部件名称：========== {text}')
                            # 将取到的下摆名字赋值到元素定位方式里面
                            self.click_get_by_title(f"{text}", exact=True, force=True)

                            self.page.wait_for_timeout(2000)

                            self.click_get_by_role(role_name=('button', '旋转180度'), force=True)
                            self.page.wait_for_timeout(500)

                finally:

                    # 获取当前的url,以确定款号
                    LoggerUtils().logger().info(f'{self.page.url}')

                    # 点击选择灵感
                    self.click_get_by_role(role_name=("tab",'1 选择灵感'),force=True)
                    try:
                        add_button = self.page.get_by_role(role="button",name='取消')
                        add_button.wait_for(timeout=500, state='visible')
                        LoggerUtils().logger().info("定位到取消按钮")

                        add_button.click()
                        self.page.wait_for_timeout(200)
                        # 点击创意组合
                        self.click_locator('//*[@id="app"]/div/section/main/aside/div/div[2]/div/div/div/img',
                                           force=True)
                        self.browser_浏览器操作(reload=True)
                        self.page.wait_for_timeout(500)
                        self.click_locator('//*[@id="app"]/div/section/main/aside/div/div[2]/div/div/div/img',
                                           force=True)
                        self.page.wait_for_timeout(300)
                        # 点击衬衫
                        self.click_get_by_role(role_name=('button', '衬衫'))
                        self.page.wait_for_timeout(300)
                        # 选择全部分类
                        self.hover_locator('//span[text()="全部 "]', num=2)
                        # 点击修身分类
                        self.click_get_by_role(role_name=("menuitem", '大阔'))
                        self.page.wait_for_timeout(500)
                        self.page.get_by_label("页", exact=True).fill(f"{j}")
                        LoggerUtils().logger().info(f"输入的页数:{j}")
                        # 跳转到指定的页数
                        self.page.get_by_label("页", exact=True).press("Enter")
                        LoggerUtils().logger().info(f"跳转的页数:{j}")
                    except Exception:
                        # 刷新浏览器页面
                        # 点击创意组合
                        self.click_locator('//*[@id="app"]/div/section/main/aside/div/div[2]/div/div/div/img',force=True)
                        # 点击衬衫
                        self.page.wait_for_timeout(300)
                        self.browser_浏览器操作(reload=True)
                        self.page.wait_for_timeout(300)
                        self.click_locator('//*[@id="app"]/div/section/main/aside/div/div[2]/div/div/div/img',force=True)

                        self.page.wait_for_timeout(500)
                        self.click_get_by_role(role_name=('button', '衬衫'))
                        self.page.wait_for_timeout(300)
                        # 选择全部分类
                        self.hover_locator('//span[text()="全部 "]', num=2)
                        # 点击修身分类
                        self.click_get_by_role(role_name=("menuitem", '大阔'))
                        self.page.wait_for_timeout(500)
                        self.page.get_by_label("页", exact=True).fill(f"{j}")
                        LoggerUtils().logger().info(f"输入的页数:{j}")
                        # 跳转到指定的页数
                        self.page.get_by_label("页", exact=True).press("Enter")
                        LoggerUtils().logger().info(f"跳转的页数:{j}")

    def T恤_修身1(self):

        # 点击快捷设计
        self.page.get_by_role("menuitem", name="快捷设计").click(force=True)
        # 点击创意组合
        self.click_locator('//*[@id="app"]/div/section/main/aside/div/div[2]/div/div/div/img',force=True)
        # 点击T恤
        self.click_get_by_role(role_name=('button', '衬衫'))
        self.page.wait_for_timeout(250)
        # 选择全部分类
        self.hover_locator('//span[text()="全部 "]', num=2)
        # 点击修身分类
        self.page.wait_for_timeout(250)
        self.click_get_by_role(role_name=("menuitem", '大阔'), force=True)
        self.page.wait_for_timeout(250)
        # 获取所有的分页数
        numb = int(self.locator('//li[@class="number"]', num=6).inner_text())
        LoggerUtils().logger().info(f'全部分页数：========== {numb}')
        # 循环所有的页数
        for j in range(1, 1520):
            # 每页循环10款
            for i in range(0, 10):
                # 点击款式
                self.click_locator(f'[id="\\3{i} "]')
                # 等待加载模型三秒
                self.page.wait_for_timeout(4500)
                try:
                    add_button = self.page.locator("//span[text()='您还没有添加装饰件']")
                    add_button.wait_for(timeout=500, state='visible')
                except Exception:
                    # 如果 "添加" 按钮不可见或不存在，则点击 "查看更多"

                    Collar_number = self.locator(
                        '//span[@class="title-num text-xl pl-[0.35rem] pr-[0.35rem]"]').inner_text()
                    pattern = r'（(\d+)）'
                    match = re.search(pattern, Collar_number)
                    number = int(match.group(1))
                    LoggerUtils().logger().info(f'领子的数量：========== {number}')

                    # 点击领子更多
                    self.click_locator('//span[text()="查看更多"]')
                    # 循环所有的领子
                    for d in range(2, number + 2):
                        # 获取所有的领子名称
                        text = self.locator('//span[@class="product-title text-base truncate ..."]', num=d).inner_text()
                        print(f'部件名称：========== {text}')
                        # 将取到的领子名字赋值到元素定位方式里面
                        self.click_get_by_title(f"{text}", exact=True, force=True)

                        # 更换完领子之后等待加载
                        self.page.wait_for_timeout(2000)
                        self.click_get_by_role(role_name=('button', '旋转180度'))
                        self.page.wait_for_timeout(500)

                    #     点击领子返回上一级
                    self.click_get_by_text(f"{Collar_number}")
                    self.page.wait_for_timeout(600)
                    try:
                        # 如果前后片存在
                        Collar_number2 = self.locator('//span[@class="title-num text-xl pl-[0.35rem] pr-[0.35rem]"]',
                                                      num=2).inner_text()
                        pattern = r'（(\d+)）'
                        match = re.search(pattern, Collar_number2)
                        number2 = int(match.group(1))
                        LoggerUtils().logger().info(f'前后上片的数量：========== {number2}')
                        # 点击前后上片
                        self.click_locator('//span[text()="查看更多"]', num=2)
                        if number2 !=1:
                            # 循环点击前后上片
                            for d in range(2, number2+2):
                                # 获取所有的前后上片名称
                                text = self.locator('//span[@class="product-title text-base truncate ..."]',num=d).inner_text()
                                print(f'部件名称：========== {text}')
                                # 将取到的前后上片名字赋值到元素定位方式里面
                                self.click_get_by_title(f"{text}",exact=True,force=True)

                                # 更换完前后上片之后等待加载
                                self.page.wait_for_timeout(2000)
                                self.click_get_by_role(role_name=('button', '旋转180度'), force=True)
                                self.page.wait_for_timeout(500)
                            # 点击前后排片返回上一级
                            self.click_get_by_text(f"{Collar_number2}", force=True)
                            self.page.wait_for_timeout(300)
                            # 点击前后排片返回上一级
                        self.click_get_by_text(f"{Collar_number2}")
                        self.page.wait_for_timeout(600)
                        Collar_number3 = self.locator(
                            '//span[@class="title-num text-xl pl-[0.35rem] pr-[0.35rem]"]',
                            num=3).inner_text()
                        pattern = r'（(\d+)）'
                        match = re.search(pattern, Collar_number3)
                        number3 = int(match.group(1))
                        LoggerUtils().logger().info(f'袖子的数量：========== {number3}')
                        self.click_locator('//span[text()="查看更多"]', num=3)
                        #  循环点击袖子
                        for d in range(2, number3 + 2):
                            # 获取所有的袖子名称
                            text = self.locator('//span[@class="product-title text-base truncate ..."]',
                                                num=d).inner_text()
                            print(f'部件名称：========== {text}')
                            # 将取到的下摆名字赋值到元素定位方式里面
                            self.click_get_by_title(f"{text}", exact=True, force=True)

                            self.page.wait_for_timeout(2000)

                            self.click_get_by_role(role_name=('button', '旋转180度'),force=True)
                            self.page.wait_for_timeout(500)

                        # 点击袖子返回上一级
                        self.click_get_by_text(f"{Collar_number3}",force=True)
                        self.page.wait_for_timeout(600)
                        Collar_number4 = self.locator(
                            '//span[@class="title-num text-xl pl-[0.35rem] pr-[0.35rem]"]',
                            num=4).inner_text()
                        pattern = r'（(\d+)）'
                        match = re.search(pattern, Collar_number4)
                        number4 = int(match.group(1))
                        LoggerUtils().logger().info(f'下摆的数量：========== {number4}')
                        self.click_locator('//span[text()="查看更多"]', num=4,force=True)
                        #  循环点击下摆
                        for d in range(2, number4 + 2):
                            # 获取所有的下摆名称
                            text = self.locator('//span[@class="product-title text-base truncate ..."]',
                                                num=d).inner_text()
                            print(f'部件名称：========== {text}')
                            # 将取到的下摆名字赋值到元素定位方式里面
                            self.click_get_by_title(f"{text}", exact=True, force=True)

                            self.page.wait_for_timeout(2000)

                            self.click_get_by_role(role_name=('button', '旋转180度'),force=True)
                            self.page.wait_for_timeout(500)



                    except Exception:

                        # 点击袖子返回上一级
                        # self.click_get_by_text(f"{Collar_number2}")
                        Collar_number5 = self.locator('//span[@class="title-num text-xl pl-[0.35rem] pr-[0.35rem]"]',
                                                      num=2).inner_text()
                        pattern = r'（(\d+)）'
                        match = re.search(pattern, Collar_number5)
                        number5 = int(match.group(1))
                        LoggerUtils().logger().info(f'袖子的数量：========== {number5}')
                        self.click_locator('//span[text()="查看更多"]', num=2,force=True)
                        #  循环点击袖子
                        for d in range(2, number5 + 2):
                            # 获取所有的袖子名称
                            text = self.locator('//span[@class="product-title text-base truncate ..."]',
                                                num=d).inner_text()
                            print(f'部件名称：========== {text}')
                            # 将取到的下摆名字赋值到元素定位方式里面
                            self.click_get_by_title(f"{text}", exact=True, force=True)

                            self.page.wait_for_timeout(2000)

                            self.click_get_by_role(role_name=('button', '旋转180度'),force=True)
                            self.page.wait_for_timeout(500)

                        # 点击袖子返回上一级
                        self.click_get_by_text(f"{Collar_number5}")
                        self.page.wait_for_timeout(300)
                        Collar_number6 = self.locator('//span[@class="title-num text-xl pl-[0.35rem] pr-[0.35rem]"]',
                                                      num=3).inner_text()
                        pattern = r'（(\d+)）'
                        match = re.search(pattern, Collar_number6)
                        number6 = int(match.group(1))
                        LoggerUtils().logger().info(f'下摆的数量：========== {number6}')
                        self.click_locator('//span[text()="查看更多"]', num=3)
                        #  循环点击下摆
                        for d in range(2, number6 + 2):
                            # 获取所有的下摆名称
                            text = self.locator('//span[@class="product-title text-base truncate ..."]',
                                                num=d).inner_text()
                            print(f'部件名称：========== {text}')
                            # 将取到的下摆名字赋值到元素定位方式里面
                            self.click_get_by_title(f"{text}", exact=True, force=True)

                            self.page.wait_for_timeout(2000)

                            self.click_get_by_role(role_name=('button', '旋转180度'),force=True)
                            self.page.wait_for_timeout(500)

                finally:

                    # 获取当前的url,以确定款号
                    LoggerUtils().logger().info(f'{self.page.url}')

                    # 点击选择灵感
                    self.click_get_by_role(role_name=("tab", '1 选择灵感'),force=True)
                    try:
                        add_button = self.page.get_by_role(role="button", name='取消')
                        add_button.wait_for(timeout=500, state='visible')
                        LoggerUtils().logger().info("定位到取消按钮")
                        add_button.click()
                        self.page.wait_for_timeout(200)
                        # 点击创意组合
                        self.click_locator('//*[@id="app"]/div/section/main/aside/div/div[2]/div/div/div/img',
                                           force=True)
                        self.browser_浏览器操作(reload=True)
                        self.page.wait_for_timeout(500)
                        self.click_locator('//*[@id="app"]/div/section/main/aside/div/div[2]/div/div/div/img',
                                           force=True)
                        # 点击衬衫
                        self.click_get_by_role(role_name=('button', '衬衫'),force=True)
                        self.page.wait_for_timeout(300)
                        # 选择全部分类
                        self.hover_locator('//span[text()="全部 "]', num=2)
                        # 点击修身分类
                        self.click_get_by_role(role_name=("menuitem", '大阔'),force=True)
                        self.page.wait_for_timeout(500)
                        self.page.get_by_label("页", exact=True).fill(f"{j}")
                        LoggerUtils().logger().info(f"输入的页数:{j}")
                        # 跳转到指定的页数
                        self.page.get_by_label("页", exact=True).press("Enter")
                        LoggerUtils().logger().info(f"跳转的页数:{j}")
                    except Exception:

                        # 刷新浏览器页面

                        # 点击创意组合
                        self.click_locator('//*[@id="app"]/div/section/main/aside/div/div[2]/div/div/div/img',force=True)
                        self.browser_浏览器操作(reload=True)
                        self.page.wait_for_timeout(200)
                        self.click_locator('//*[@id="app"]/div/section/main/aside/div/div[2]/div/div/div/img',
                                           force=True)
                        self.page.wait_for_timeout(500)
                        # 点击T恤
                        self.click_get_by_role(role_name=('button', '衬衫'),force=True)
                        self.page.wait_for_timeout(300)

                        # 选择全部分类
                        self.hover_locator('//span[text()="全部 "]', num=2)
                        # 点击修身分类
                        self.click_get_by_role(role_name=("menuitem", '大阔'),force=True)
                        self.page.wait_for_timeout(500)
                        self.page.get_by_label("页", exact=True).fill(f"{j}")
                        LoggerUtils().logger().info(f"输入的页数:{j}")
                        # 跳转到指定的页数
                        self.page.get_by_label("页", exact=True).press("Enter")
                        LoggerUtils().logger().info(f"跳转的页数:{j}")






